package com.opesoft.pf.jpa;

import java.util.ArrayList;
import java.util.List;
import com.opesoft.fw.core.utils.SQLUtils;

public class SimpleQL
{
	String pojo; // 要操作的Pojo
	String alias="x"; // 别名
	String[] fields; // 查询的字段
	String[] orderBy; // Order by子句
	List<Condition> conditions=new ArrayList<Condition>(); // 条件子句中的条件

	public SimpleQL(String pojo)
	{
		this.pojo=pojo;
	}

	public void addStringCondition(String field,String op,String value)
	{
		if(!SQLUtils.isNull(value)) this.conditions.add(new Condition(field,op,value,Condition.STRING_TYPE));
	}

	public void addNumCondition(String field,String op,String value)
	{
		if(!SQLUtils.isNull(value)&&!"null".equalsIgnoreCase(value)) this.conditions.add(new Condition(field,op,value,Condition.NUM_TYPE));
	}

	public void addStringLikeCondition(String field,String value)
	{
		if(!SQLUtils.isNull(value)) this.conditions.add(new Condition(field,"like","%"+value+"%",Condition.STRING_TYPE));
	}

	public void addNumInCondition(String field,Object[] values)
	{
		if(values!=null&&values.length>0)
		{
			StringBuffer sb=new StringBuffer();
			for(int i=0;i<values.length;i++)
			{
				sb.append(values[i]+"");
				if(i<values.length-1) sb.append(",");
			}

			this.conditions.add(new Condition(field,"in","("+sb.toString()+")",Condition.CONDITION));
		}
	}

	/**
	 * 生成用于统计数量的查询语句
	 * 
	 * @return
	 */
	public String countQL()
	{
		StringBuffer hql=new StringBuffer();

		hql.append("select count("+this.alias+")");
		hql.append(" from "+this.pojo+" "+this.alias);

		parseConditions(hql);

		return hql.toString();
	}

	private void parseConditions(StringBuffer hql)
	{
		if(this.conditions.size()>0)
		{
			hql.append(" where");
			for(int i=0;i<this.conditions.size();i++)
			{
				Condition cond=this.conditions.get(i);
				hql.append(" "+cond.hql(this.alias));
				if(i<(this.conditions.size()-1)) hql.append(" and");
			}
		}
	}

	/**
	 * 生成用于查询的语句
	 * 
	 * @return
	 */
	public String queryQL()
	{
		StringBuffer hql=new StringBuffer();

		hql.append("select ");
		if(this.fields==null||this.fields.length==0) hql.append(this.alias);
		else
		{
			for(int i=0;i<this.fields.length;i++)
			{
				hql.append(this.alias+"."+this.fields[i]);
				if(i<(this.fields.length-1)) hql.append(" , ");
			}
		}

		hql.append(" from "+this.pojo+" "+this.alias);

		parseConditions(hql);

		if(this.orderBy!=null&&this.orderBy.length>0)
		{
			hql.append(" order by ");
			for(int i=0;i<this.orderBy.length;i++)
			{
				String field=this.orderBy[i];
				hql.append(this.alias+"."+field);
				if(i<(this.orderBy.length-1)) hql.append(" , ");
			}
		}

		return hql.toString();
	}

	public String toString()
	{
		return this.queryQL();

	}

	public String getAlias()
	{
		return alias;
	}

	public void setAlias(String alias)
	{
		this.alias=alias;
	}

	public String[] getOrderBy()
	{
		return orderBy;
	}

	public void setOrderBy(String[] orderBy)
	{
		this.orderBy=orderBy;
	}

	public String[] getFields()
	{
		return fields;
	}

	public void setFields(String[] fields)
	{
		this.fields=fields;
	}
}

class Condition
{
	String field; // 字段名
	String op; // 操作符
	String value; // 值
	int type; // 值的类型
	static int STRING_TYPE=1; // 字符串类型
	static int NUM_TYPE=2; // 数字类型
	static int CONDITION=9; // 本身就是一个条件

	public Condition(String field,String op,String value,int type)
	{
		this.field=field;
		this.op=op;
		this.value=value;
		this.type=type;
	}

	public String hql()
	{
		String hql=this.field+" "+this.op;
		if(this.type==Condition.STRING_TYPE) hql+=" '"+this.value+"'";
		else hql+=" "+this.value;
		return hql;
	}

	public String hql(String alias)
	{
		return alias+"."+this.hql();
	}
}
